package org.study.bookmanagerclientbyswing.api;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.study.bookmanagerclientbyswing.io.request.Book.AddBookReq;
import org.study.bookmanagerclientbyswing.io.request.Book.AddBookTypeReq;
import org.study.bookmanagerclientbyswing.io.request.Book.GetBooksReq;
import org.study.bookmanagerclientbyswing.io.request.Book.UpdateBookReq;
import org.study.bookmanagerclientbyswing.io.response.RestBean;

@Component
public class BookApi {

    @Resource
    private WebClient webClient;

    private String baseUrl = "v1/book";

    public RestBean addBookType(AddBookTypeReq req) {
        return webClient.post()
                .uri(baseUrl+"/type")
                .bodyValue(req)
                .retrieve()
                .bodyToMono(RestBean.class)
                .block();
    }

    public RestBean addBook(AddBookReq req) {
        return webClient.post()
                .uri(baseUrl+"/")
                .bodyValue(req)
                .retrieve()
                .bodyToMono(RestBean.class)
                .block();
    }

    public RestBean deleteBook(int barcodeId) {
        return webClient.delete()
                .uri(uriBuilder -> uriBuilder.queryParam("barcodeId", barcodeId).build())
                .retrieve()
                .bodyToMono(RestBean.class)
                .block();
    }

    public RestBean updateBook(UpdateBookReq req) {
        return webClient.put()
                .uri(baseUrl+"/")
                .bodyValue(req)
                .retrieve()
                .bodyToMono(RestBean.class)
                .block();
    }

    public RestBean findBooks(GetBooksReq req) {
        return webClient.post()
                .uri(baseUrl+"/list")
                .bodyValue(req)
                .retrieve()
                .bodyToMono(RestBean.class)
                .block();
    }
}
